﻿using System.Collections.Generic;
using System.Data;
using Model;
using Utils;
namespace Dao
{
    public class OracleDaoBase : IDaoBase
    {
        public List<ColumnModel> GetColumnList()
        {
            // OracleHelper.ConnectionString = "";
            DataTable dt = OracleHelper.ExecuteDataTable("select com.COMMENTS,com.COLUMN_NAME,col.DATA_TYPE,col.DATA_LENGTH from user_tab_columns col join user_col_comments com on  col.table_name=com.table_name and col.column_name=com.column_name    where com.Table_Name='" + TableName.ToUpper() + "'");
           
            var primaryLst = GetIsPrimary(TableName);
            var lst = new List<ColumnModel>();
            var dtConfig = new DatatypeConfig();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                ColumnModel column = new ColumnModel();
                string comment = dr["COMMENTS"].ToString();
                string name = dr["COLUMN_NAME"].ToString();
                string type = dtConfig.GetDataType(dr["DATA_TYPE"].ToString());
                column.ColumnMemo = comment == "" ? name : comment;
                column.ColumnName = name;
                int maxLen = 0;
                int.TryParse(dr["DATA_LENGTH"].ToString(), out maxLen);
                column.ColumnMaxLen = maxLen;
                column.ColumnType = type;
                column.IsPrimary = primaryLst.Contains(name);
                column.ColumnNameAlias = StringUtils.GetAlias(name);
                lst.Add(column);
            }
            return lst;
        }
        /// <summary>
        /// 返回当前表的主键
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        private List<string> GetIsPrimary(string tableName)
        {
            string sql =
                string.Format(
                    "select b.column_name from user_constraints a join user_ind_columns b on a.index_name = b.index_name and b.table_name = '{0}' and a.constraint_type = 'P'", tableName.ToUpper());
           
 DataTable dt = OracleHelper.ExecuteDataTable(sql);
            var lst = new List<string>();
            foreach (DataRow dr in dt.Rows)
            {
                lst.Add(dr["column_name"].ToString());
            }
            return lst;
        }
        /// <summary>
        /// 返回表信息
        /// </summary>
        /// <returns></returns>
        public List<TableModel> GetTable()
        {
            string sql = string.Format("SELECT t.table_name,co.comments FROM USER_TABLES t join user_tab_comments co on t.table_name=co.table_name");
            if (!string.IsNullOrEmpty(TableName))
            {
                sql += string.Format(" where t.table_name='{0}'", TableName.ToUpper());
            }
            DataTable dt = OracleHelper.ExecuteDataTable(sql);
            var lst = new List<TableModel>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                TableModel table = new TableModel();
                table.TableName = dr["TABLE_NAME"].ToString();
                table.TableMemo = dr["comments"].ToString() == "" ? TableName : dr["comments"].ToString();
                table.TableNameAlias = StringUtils.GetAlias(table.TableName);
                lst.Add(table);
            }
            return lst;
        }

        public string TableName { get; set; }
        public string PrimaryKey { get; set; }
        /// <summary>
        /// 数据库列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetDbList()
        {
            DataTable dt = OracleHelper.ExecuteDataTable("show databases;");
            var lst = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                lst.Add(dr["database"].ToString());
            }
            return lst;
        }
    }
}
